/*
 * @Description: useEffect 内闭包陷阱
 * @Date: 2020-12-14 17:54:50
 * @LastEditors: mashilei@meituan.com
 * @LastEditTime: 2020-12-14 18:42:06
 * @FilePath: /react-app-ts/src/views/Count.tsx
 */
import { useState, useEffect } from 'react';

const Counter = () => {
    let [count, setCount] = useState(0);
    useEffect(() => {
        const id = setInterval(() => {
            // console.log(count, 'count') // 由于闭包的原因，count 永远是 0
            setCount(count => count + 1)

        }, 1000);
        return () => clearInterval(id);
    }, [])
    return (
        <div>
            <h2>{count}</h2>
        </div>
    )
}

export default Counter;